Conditions | 2 |
Total Lines | 233 |
Code Lines | 88 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* |
||
48 | */ |
||
49 | toBuffer() { |
||
50 | this.validateWavHeader(); |
||
51 | return this.writeWavBuffer_(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Return the sample at a given index. |
||
56 | * @param {number} index The sample index. |
||
57 | * @return {number} The sample. |
||
58 | * @throws {Error} If the sample index is off range. |
||
59 | */ |
||
60 | getSample(index) { |
||
61 | index = index * (this.dataType.bits / 8); |
||
62 | if (index + this.dataType.bits / 8 > this.data.samples.length) { |
||
63 | throw new Error('Range error'); |
||
64 | } |
||
65 | return unpack( |
||
66 | this.data.samples.slice(index, index + this.dataType.bits / 8), |
||
67 | this.dataType); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Set the sample at a given index. |
||
72 | * @param {number} index The sample index. |
||
73 | * @param {number} sample The sample. |
||
74 | * @throws {Error} If the sample index is off range. |
||
75 | */ |
||
76 | setSample(index, sample) { |
||
77 | index = index * (this.dataType.bits / 8); |
||
78 | if (index + this.dataType.bits / 8 > this.data.samples.length) { |
||
79 | throw new Error('Range error'); |
||
80 | } |
||
81 | packTo(sample, this.dataType, this.data.samples, index); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Validate the header of the file. |
||
86 | * @throws {Error} If bit depth is invalid. |
||
87 | * @throws {Error} If the number of channels is invalid. |
||
88 | * @throws {Error} If the sample rate is invalid. |
||
89 | * @protected |
||
90 | */ |
||
91 | validateWavHeader() { |
||
92 | this.validateBitDepth_(); |
||
93 | if (!validateNumChannels(this.fmt.numChannels, this.fmt.bitsPerSample)) { |
||
94 | throw new Error('Invalid number of channels.'); |
||
95 | } |
||
96 | if (!validateSampleRate( |
||
97 | this.fmt.numChannels, this.fmt.bitsPerSample, this.fmt.sampleRate)) { |
||
98 | throw new Error('Invalid sample rate.'); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Return a .wav file byte buffer with the data from the WaveFileParser object. |
||
104 | * The return value of this method can be written straight to disk. |
||
105 | * @return {!Uint8Array} The wav file bytes. |
||
106 | * @private |
||
107 | */ |
||
108 | writeWavBuffer_() { |
||
109 | this.uInt16_.be = this.container === 'RIFX'; |
||
110 | this.uInt32_.be = this.uInt16_.be; |
||
111 | /** @type {!Array<!Array<number>>} */ |
||
112 | let fileBody = [ |
||
113 | this.getJunkBytes_(), |
||
114 | this.getDs64Bytes_(), |
||
115 | this.getBextBytes_(), |
||
116 | this.getFmtBytes_(), |
||
117 | this.getFactBytes_(), |
||
118 | packString(this.data.chunkId), |
||
119 | pack(this.data.samples.length, this.uInt32_), |
||
120 | this.data.samples, |
||
121 | this.getCueBytes_(), |
||
122 | this.getSmplBytes_(), |
||
123 | this.getLISTBytes_() |
||
124 | ]; |
||
125 | /** @type {number} */ |
||
126 | let fileBodyLength = 0; |
||
127 | for (let i=0; i<fileBody.length; i++) { |
||
128 | fileBodyLength += fileBody[i].length; |
||
129 | } |
||
130 | /** @type {!Uint8Array} */ |
||
131 | let file = new Uint8Array(fileBodyLength + 12); |
||
132 | /** @type {number} */ |
||
133 | let index = 0; |
||
134 | index = packStringTo(this.container, file, index); |
||
135 | index = packTo(fileBodyLength + 4, this.uInt32_, file, index); |
||
136 | index = packStringTo(this.format, file, index); |
||
137 | for (let i=0; i<fileBody.length; i++) { |
||
138 | file.set(fileBody[i], index); |
||
139 | index += fileBody[i].length; |
||
140 | } |
||
141 | return file; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Return the bytes of the 'bext' chunk. |
||
146 | * @private |
||
147 | */ |
||
148 | getBextBytes_() { |
||
149 | /** @type {!Array<number>} */ |
||
150 | let bytes = []; |
||
151 | this.enforceBext_(); |
||
152 | if (this.bext.chunkId) { |
||
153 | this.bext.chunkSize = 602 + this.bext.codingHistory.length; |
||
154 | bytes = bytes.concat( |
||
155 | packString(this.bext.chunkId), |
||
156 | pack(602 + this.bext.codingHistory.length, this.uInt32_), |
||
157 | writeString(this.bext.description, 256), |
||
158 | writeString(this.bext.originator, 32), |
||
159 | writeString(this.bext.originatorReference, 32), |
||
160 | writeString(this.bext.originationDate, 10), |
||
161 | writeString(this.bext.originationTime, 8), |
||
162 | pack(this.bext.timeReference[0], this.uInt32_), |
||
163 | pack(this.bext.timeReference[1], this.uInt32_), |
||
164 | pack(this.bext.version, this.uInt16_), |
||
165 | writeString(this.bext.UMID, 64), |
||
166 | pack(this.bext.loudnessValue, this.uInt16_), |
||
167 | pack(this.bext.loudnessRange, this.uInt16_), |
||
168 | pack(this.bext.maxTruePeakLevel, this.uInt16_), |
||
169 | pack(this.bext.maxMomentaryLoudness, this.uInt16_), |
||
170 | pack(this.bext.maxShortTermLoudness, this.uInt16_), |
||
171 | writeString(this.bext.reserved, 180), |
||
172 | writeString( |
||
173 | this.bext.codingHistory, this.bext.codingHistory.length)); |
||
174 | } |
||
175 | return bytes; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Make sure a 'bext' chunk is created if BWF data was created in a file. |
||
180 | * @private |
||
181 | */ |
||
182 | enforceBext_() { |
||
183 | for (let prop in this.bext) { |
||
184 | if (this.bext.hasOwnProperty(prop)) { |
||
185 | if (this.bext[prop] && prop != 'timeReference') { |
||
186 | this.bext.chunkId = 'bext'; |
||
187 | break; |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | if (this.bext.timeReference[0] || this.bext.timeReference[1]) { |
||
192 | this.bext.chunkId = 'bext'; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Return the bytes of the 'ds64' chunk. |
||
198 | * @return {!Array<number>} The 'ds64' chunk bytes. |
||
199 | * @private |
||
200 | */ |
||
201 | getDs64Bytes_() { |
||
202 | /** @type {!Array<number>} */ |
||
203 | let bytes = []; |
||
204 | if (this.ds64.chunkId) { |
||
205 | bytes = bytes.concat( |
||
206 | packString(this.ds64.chunkId), |
||
207 | pack(this.ds64.chunkSize, this.uInt32_), |
||
208 | pack(this.ds64.riffSizeHigh, this.uInt32_), |
||
209 | pack(this.ds64.riffSizeLow, this.uInt32_), |
||
210 | pack(this.ds64.dataSizeHigh, this.uInt32_), |
||
211 | pack(this.ds64.dataSizeLow, this.uInt32_), |
||
212 | pack(this.ds64.originationTime, this.uInt32_), |
||
213 | pack(this.ds64.sampleCountHigh, this.uInt32_), |
||
214 | pack(this.ds64.sampleCountLow, this.uInt32_)); |
||
215 | } |
||
216 | //if (this.ds64.tableLength) { |
||
217 | // ds64Bytes = ds64Bytes.concat( |
||
218 | // pack(this.ds64.tableLength, this.uInt32_), |
||
219 | // this.ds64.table); |
||
220 | //} |
||
221 | return bytes; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Return the bytes of the 'cue ' chunk. |
||
226 | * @return {!Array<number>} The 'cue ' chunk bytes. |
||
227 | * @private |
||
228 | */ |
||
229 | getCueBytes_() { |
||
230 | /** @type {!Array<number>} */ |
||
231 | let bytes = []; |
||
232 | if (this.cue.chunkId) { |
||
233 | /** @type {!Array<number>} */ |
||
234 | let cuePointsBytes = this.getCuePointsBytes_(); |
||
235 | bytes = bytes.concat( |
||
236 | packString(this.cue.chunkId), |
||
237 | pack(cuePointsBytes.length + 4, this.uInt32_), |
||
238 | pack(this.cue.dwCuePoints, this.uInt32_), |
||
239 | cuePointsBytes); |
||
240 | } |
||
241 | return bytes; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Return the bytes of the 'cue ' points. |
||
246 | * @return {!Array<number>} The 'cue ' points as an array of bytes. |
||
247 | * @private |
||
248 | */ |
||
249 | getCuePointsBytes_() { |
||
250 | /** @type {!Array<number>} */ |
||
251 | let points = []; |
||
252 | for (let i=0; i<this.cue.dwCuePoints; i++) { |
||
253 | points = points.concat( |
||
254 | pack(this.cue.points[i].dwName, this.uInt32_), |
||
255 | pack(this.cue.points[i].dwPosition, this.uInt32_), |
||
256 | packString(this.cue.points[i].fccChunk), |
||
257 | pack(this.cue.points[i].dwChunkStart, this.uInt32_), |
||
258 | pack(this.cue.points[i].dwBlockStart, this.uInt32_), |
||
259 | pack(this.cue.points[i].dwSampleOffset, this.uInt32_)); |
||
260 | } |
||
261 | return points; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Return the bytes of the 'smpl' chunk. |
||
266 | * @return {!Array<number>} The 'smpl' chunk bytes. |
||
267 | * @private |
||
268 | */ |
||
269 | getSmplBytes_() { |
||
270 | /** @type {!Array<number>} */ |
||
271 | let bytes = []; |
||
272 | if (this.smpl.chunkId) { |
||
273 | /** @type {!Array<number>} */ |
||
274 | let smplLoopsBytes = this.getSmplLoopsBytes_(); |
||
275 | bytes = bytes.concat( |
||
276 | packString(this.smpl.chunkId), |
||
277 | pack(smplLoopsBytes.length + 36, this.uInt32_), |
||
278 | pack(this.smpl.dwManufacturer, this.uInt32_), |
||
279 | pack(this.smpl.dwProduct, this.uInt32_), |
||
280 | pack(this.smpl.dwSamplePeriod, this.uInt32_), |
||
281 | pack(this.smpl.dwMIDIUnityNote, this.uInt32_), |
||
498 |